/* utils*/
import { getRootWindow } from '@src/util/dom';
import AuthUtil from "@src/util/auth";
import { openAccurateTab } from "@src/util/platform";
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { getRootWindowInitData } from '@src/util/window'
import { ctiCallOut } from '@src/util/ctisdk';
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
/* components*/
import BaseTableCell from 'packages/BaseTableCell';
/* mixin */
import ConnectorMixin from '@src/mixins/connectorMixin'
import { VersionControlTaskMixin } from '@src/mixins/versionControlMixin'
import LogisticsMixin from '@src/mixins/logisticsMixin'

export default {
    name: 'event-list-mixin',
    mixins: [
        ConnectorMixin,
        VersionControlTaskMixin,
        LogisticsMixin,
    ],
    data() {
        return {
            auth: {},
            initData: {},
        }
    },
    computed: {
        /*查看事件详情的权限*/
        allowEventViewDetail() {
            return AuthUtil.hasAuth(this.auth, 'CASE_VIEW_DETAIL')
        },

        /*灰度满意度方案*/
        customerSatisfaction() {
            return getRootWindowInitData()?.customerSatisfaction || false;
        },

        hasCallAuth() {
            return !!getRootWindow(window).CTI
        },

        /** 判断呼叫中心开启电话 */
        callCenterModule() {
            return localStorage.getItem('call_center_module') && Number(localStorage.getItem('call_center_module'))
        },

    },
    components: {
        [BaseTableCell.name]: BaseTableCell,
    },
    methods: {
        // 获取window._init auth
        getAuthByRootInitData() {
            const rootWindow = getRootWindow(window)
            const _initData = JSON.parse(rootWindow?._init || '{}')
            this.auth = _initData?.user?.auth || {}
        },

        /*查看事件详情*/
        openEventDetailTab(eventId, eventNo) {
            if (!eventId || !this.allowEventViewDetail) return;
            let fromId = window.frameElement.getAttribute('id');
            openAccurateTab({
                type:PageRoutesTypeEnum.PageEventView,
                key:eventId,
                titleKey:eventNo,
                fromId
            })
        },

        /**
         * @description: 如果有evaluateConfig.useStarEvaluate 构建额外的列表项
         * @return {Array} extraColumns
         */
        buildExtraEvaluateColumns(defaultShow = true) {
            const { evaluateConfig={} } = this.initData
            const extraColumns = evaluateConfig.starEvaluates.map((starEvaluate, index) => {
                const field = {
                    fieldName: `starEvaluate${index+1}`,
                    displayName: starEvaluate,
                    renderCell: (h, col, row) => {
                        const evaluateText = row.evaluate ? row.evaluate[`starEvaluate${index+1}`] : ''
                        return (<div>{evaluateText}</div>)
                    },
                    formType: 'text',
                    setting:  {},
                    isSystem: 1,
                    isNull: 1,
                    isSearch: 1,
                    orderId: 1,
                    show: defaultShow,
                    tableName: 'event',
                    visable: true
                }
                return formatColumn(field);
            })
            return extraColumns
        },

        /*获取userIds*/
        getUserIds (value) {
            // 多选
            if (Array.isArray(value)) {
                return value.map((i) => i.staffId);
            }

            let user = value || {};
            return [user.staffId];
        },

        /*处理人员显示*/
        getUserName(value) {
            // 多选
            if(Array.isArray(value)) {
                return value.map(i => i.displayName || i.name).join(",");
            }

            let user = value || {};
            return user.displayName || user.name;
        },

        /*拨打电话*/
        async makePhoneCall(phone) {
            try {
                ctiCallOut(phone)
            } catch (error) {
                console.error(error);
            }
        },

        /*打开富文本弹窗*/
        openRichtextVisible(row, column) {
            const richtextId = row?.attribute?.[column.fieldName] || ''
            this.$refs?.baseViewRichTextRef?.openDialog(richtextId)
        },

        /*处理自定义字段*/
        handlerCustomFields(fields) {
            return fields?.filter(item => item.isSystem == 0)?.map((field) => {
                field.group = field.tableName;
                field.label = field.displayName;
                field.field = field.fieldName;
                if(field.formType === 'date' || field.formType === 'datetime') {
                    field.sortable = 'custom'
                }
                return field
            }) ?? [];
        },
    }
}